package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi{

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo getById(Long userID) {
        return userInfoMapper.selectById(userID);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,ids);

        if (userInfo!=null){
            queryWrapper.lt(userInfo.getAge()!=null,UserInfo::getAge,userInfo.getAge());
            queryWrapper.eq(userInfo.getGender()!=null,UserInfo::getGender,userInfo.getGender());
            queryWrapper.like(StrUtil.isNotEmpty(userInfo.getNickname()),UserInfo::getNickname,userInfo.getNickname());
        }

        List<UserInfo> list = userInfoMapper.selectList(queryWrapper);

        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");

        return map;
    }
}
